package com.neusoft.neuiotms.sap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.bizcore.web.utils.JsonUtils;
import com.neusoft.neuiotms.dcam.fixed.converter.AssetConverter;
import com.neusoft.neuiotms.dcam.fixed.converter.StockConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.StockDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Asset;
import com.neusoft.neuiotms.dcam.fixed.model.Stock;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.StockRepository;
import com.neusoft.neuiotms.dcam.fixed.service.AssetService;
import com.neusoft.neuiotms.dcam.fixed.service.StockService;
import com.neusoft.neuiotms.dcam.fixed.service.spareconsume.SpareConsumeStockService;
import com.neusoft.neuiotms.micro.dcam.DcamMicroService;

@Service
public class DcamMicroServiceImpl implements DcamMicroService {

    @Autowired
    private AssetRepository assetRepository;
    @Autowired
    private AssetConverter assetConventer;
    @Autowired
    private StockRepository stockRepository;
    @Autowired
    private StockConverter stockConverter;
    @Autowired
    private StockService stockService;
    @Autowired
    private AssetService assetService;
    @Autowired
    private SpareConsumeStockService spareConsumeStockService;

    @Override
    public ResultListDTO<Map<String, Object>> AssetByCodes(final Map<String, String[]> assetCodes) {

        final String[] codes = assetCodes.get("assetCodes");

        if (null == codes) {
            return ResultListDTO.success(new ArrayList<>());
        }
        final List<Asset> models = new ArrayList<>();
        Arrays.asList(codes).stream().forEach(item -> {
            final Asset model = this.assetRepository.findByCode(item);
            models.add(model);
        });
        final List<AssetDTO> dtos = this.assetConventer.toListDTO(models);
        //        final List<Map<String, Object>> list =
        //                dtos.stream().map(dto -> JsonUtils.pojoToMap(AssetDTO.class)).collect(Collectors.toList());
        final List<Map<String, Object>> list =
                dtos.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());

        return ResultListDTO.success(list);
    }

    @Override
    public ResultListDTO<Map<String, Object>> StocksByIds(final Map<String, Long[]> stockIds) {

        final Long[] ids = stockIds.get("stockIds");

        if (null == ids) {
            return ResultListDTO.success(new ArrayList<>());
        }

        final List<Stock> models = this.stockRepository.findAllById(Arrays.asList(ids));
        final List<StockDTO> dtos = this.stockConverter.toListDTO(models);
        final List<Map<String, Object>> list =
                dtos.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());

        return ResultListDTO.success(list);
    }

    @Override
    public ResultDTO<Map<String, Object>> updateStockNum(final Long id, final int num) {
        final StockDTO dto = this.stockService.updateStockNum(id, num);
        return ResultDTO.success(JsonUtils.pojoToMap(dto));
    }

    @Override
    public ResultListDTO<Map<String, Object>> searchAssets(final Map<String, Object> queryMap) {

        final Searchable searchable = new Searchable();
        if (null != queryMap) {
            queryMap.entrySet().forEach(entry -> {
                searchable.put(entry.getKey().replace("s_", ""), entry.getValue());
            });
        }
        final List<AssetDTO> dtos = this.assetService.findAll(searchable);

        final List<Map<String, Object>> list =
                dtos.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @Override
    public ResultDTO<Long> searchAssetsCount(final Map<String, Object> queryMap) {
        final Searchable searchable = new Searchable();
        if (null != queryMap) {
            queryMap.entrySet().forEach(entry -> {
                searchable.put(entry.getKey().replace("s_", ""), entry.getValue());
            });
        }
        final Long count = this.assetService.count(searchable);
        return ResultDTO.success(count);
    }

    @Override
    public ResultListDTO<Map<String, Object>> searchFinishStockListByWorknum(final String workNum) {
        final List<StockDTO> dtos = this.spareConsumeStockService.finishList(workNum);
        final List<Map<String, Object>> list =
                dtos.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @Override
    public ResultDTO<Boolean> updateAssetRunningStatus(boolean status, Long[] idArray) {
        final boolean result = this.assetService.updateAssetRunningStatus(status, idArray);
        return ResultDTO.success(result);
    }

    @Override
    public ResultListDTO<Map<String, Object>> searchFinishStockListByWorknumAndConfirmUser(String workNum,
            String confirmUser) {
        final List<StockDTO> dtos = this.spareConsumeStockService.finishList(workNum, confirmUser);
        final List<Map<String, Object>> list =
                dtos.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

}
